﻿<%@ WebHandler Language="C#" Class="resourceAjax" %>

using System;
using System.Web;

public class resourceAjax : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        try
        {
            string act = "";
            if (context.Request.Form["act"] != null && context.Request.Form["act"].Length > 0)
                act = context.Server.UrlDecode(context.Request.Form["act"]);
            switch (act)
            { 
                case "delete_dir":
                    DeleteDir(context);
                    break;
                case "delete_file":
                    DeleteFile(context);
                    break;
                case "new_dir":
                    NewDir(context);
                    break;
                default:
                    LoadResource(context);
                    break;
            }
        }

        catch (Exception ex)
        {
            context.Response.Write("Error: " + ex.Message);
        }
    }

    void NewDir(HttpContext context)
    {
        string dir = "";
        if (context.Request.Form["dir"] != null && context.Request.Form["dir"].Length > 0)
            dir = context.Server.UrlDecode(context.Request.Form["dir"]);

        System.IO.Directory.CreateDirectory(context.Server.MapPath("~" + dir));
    }

    void DeleteDir(HttpContext context)
    {
        string link = "";
        if (context.Request.Form["link"] != null && context.Request.Form["link"].Length > 0)
            link = context.Server.UrlDecode(context.Request.Form["link"]);        
        
        System.IO.Directory.Delete(context.Server.MapPath("~"+link),true);
    }

    void DeleteFile(HttpContext context)
    {
        string link = "";
        if (context.Request.Form["link"] != null && context.Request.Form["link"].Length > 0)
            link = context.Server.UrlDecode(context.Request.Form["link"]);

        System.IO.File.Delete(context.Server.MapPath("~/" + link));
    }

    void LoadResource(HttpContext context)
    {
        string path_img = GetApplicationPath(context);
        string[] extArr = new string[] { "jpg", "png", "gif", "jpeg", "", "", "" };
        string dir;        
        if (context.Request.Form["dir"] == null || context.Request.Form["dir"].Length <= 0)
            dir = "Uploads/";
        else
            dir = context.Server.UrlDecode(context.Request.Form["dir"]);        

        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(context.Server.MapPath("~/" + dir));

        if (di.Name != "Uploads")
            context.Response.Write("\t<div class=\"file directory\"><a class=\"file_top\" href=\"#\" rel=\"" + dir.Replace(di.Name + "/", "") + "\"></a><div class=\"title\" style=\"text-align:center\">Thư mục cha</div></div>\n");
        foreach (System.IO.DirectoryInfo di_child in di.GetDirectories())
            context.Response.Write("\t<div class=\"file directory\"><a class=\"file_top\" href=\"#\" rel=\"" + dir + di_child.Name + "/\"></a><div class=\"title\"><a rel=\"" + dir + di_child.Name + "/\" class=\"delete_tool xoa_thumuc\" href=\"#\" ></a>" + ((di_child.Name.Length > 12) ? di_child.Name.Substring(0, 12) + "..." : di_child.Name) + "</div></div>\n");

        foreach (System.IO.FileInfo fi in di.GetFiles())
        {
            string ext = "";
            if (fi.Extension.Length > 1)
                ext = fi.Extension.Substring(1).ToLower();

            if (Array.IndexOf(extArr, ext) >= 0)
                context.Response.Write("\t<div class=\"file ext_" + ext + "\"><a class=\"file_top\" href=\"#\" rel=\"" + path_img + dir.Remove(0, 1) + fi.Name + "\"><img src=\"" + path_img + dir.Remove(0, 1) + fi.Name + "\"/></a><div class=\"title\"><a rel=\"" + dir.Remove(0, 1) + fi.Name + "\" class=\"delete_tool xoa_taptin\" href=\"#\"></a>" + ((fi.Name.Length > 12) ? fi.Name.Substring(0, 12) + "..." : fi.Name) + "</div></div>\n");
            else
                context.Response.Write("\t<div class=\"file ext_" + ext + "\"><a class=\"file_top\" href=\"#\" rel=\"" + path_img + dir.Remove(0, 1) + fi.Name + "\"></a><div class=\"title\"><a rel=\"" + dir.Remove(0, 1) + fi.Name + "\" class=\"delete_tool xoa_taptin\" href=\"#\"></a>" + ((fi.Name.Length > 12) ? fi.Name.Substring(0, 12) + "..." : fi.Name) + "</div></div>\n");
        }
    }

    public string GetApplicationPath(HttpContext context)
    {
        string applicationPath = "";

        if (context.Request.Url != null)
            applicationPath = context.Request.Url.AbsoluteUri.Substring(
             0, context.Request.Url.AbsoluteUri.ToLower().IndexOf(
              context.Request.ApplicationPath.ToLower(),
               context.Request.Url.AbsoluteUri.ToLower().IndexOf(
              context.Request.Url.Authority.ToLower()) +
              context.Request.Url.Authority.Length) +
             context.Request.ApplicationPath.Length);
        return applicationPath;
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}